package cn.tedu.hotel.service.impl;

import cn.tedu.hotel.ex.ServiceException;
import cn.tedu.hotel.mapper.AdminRoleMapper;
import cn.tedu.hotel.mapper.RoleMapper;
import cn.tedu.hotel.pojo.dto.AdminRoleAddNewDTO;
import cn.tedu.hotel.pojo.dto.RoleAddNewDTO;
import cn.tedu.hotel.pojo.entity.AdminRole;
import cn.tedu.hotel.pojo.entity.Role;
import cn.tedu.hotel.pojo.vo.AdminRoleStandardVO;
import cn.tedu.hotel.pojo.vo.RoleListItemVO;
import cn.tedu.hotel.pojo.vo.RoleStandardVO;
import cn.tedu.hotel.service.IRoleService;
import cn.tedu.hotel.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements IRoleService, ServiceCode {

    public RoleServiceImpl(){
        log.debug("创建业务对象：RoleServiceImpl");
    }

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    AdminRoleMapper adminRoleMapper;

    @Override
    public void addNew(RoleAddNewDTO roleAddNewDTO) {
        log.debug("开始处理【添加角色】的业务，参数：{}",roleAddNewDTO);
        // 获取角色名称
        String name = roleAddNewDTO.getName();
        //检查表中是否存在角色数据
        int count = roleMapper.countByName(name);
        if (count > 0){
            String message = "添加失败，该角色【"+name+"】已存在";
            log.warn(message);
            throw new ServiceException(ERR_CONFLICT,message);
        }

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 创建role对象
        Role role = new Role();
        //补全属性值
        BeanUtils.copyProperties(roleAddNewDTO,role);
        role.setGmtCreate(now);
        role.setGmtModified(now);

        //执行插入数据
        int rows = roleMapper.insert(role);
        if (rows != 1){
            String message = "添加失败，服务器繁忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除id={}的角色数据】",id);
        // 检查角色数据是否存在
        RoleStandardVO queryResult = roleMapper.getStandardById(id);
        if (queryResult == null){
            String message = "删除失败，访问数据不存在";
            log.warn(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }

        AdminRole adminRole = new AdminRole();
        Long sysAdminId = adminRole.getSysAdminId();
        Long sysRoleId = adminRole.getSysRoleId();
        // 检查是否存在关联管理员
        int count = adminRoleMapper.countByAdminAndRole(sysAdminId, sysRoleId);
        if (count > 0){
            String message = "删除失败，角色存在关联关系";
            log.warn(message);
            throw new ServiceException(ERR_DELETE,message);
        }

        //执行删除
        int rows = roleMapper.deleteById(id);
        if (rows != 1){
            String message = "删除失败，服务器繁忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ERR_DELETE,message);
        }

    }

    @Override
    public List<RoleListItemVO> list() {
        log.debug("开始处理【查询角色列表】的业务");
        return roleMapper.list();
    }
}
